﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using VQData;
using System.Web.Script.Serialization;
using VQWEB.common;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for AdvertisementsHandler
    /// </summary>
    public class core_AdvertisementsHandler : IHttpHandler, IRouteHandler
    {
        AdvertisementData AdvertisementData = new AdvertisementData();
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Advertisement     
            string add_advertisement = context.Request["add_advertisement"];
            if (!string.IsNullOrEmpty(add_advertisement))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                var obj = bussiness.InsertOrUpdate<core_advertisement>(add_advertisement);
                context.Response.Write("Thực thi thành công !");         
            }
            string view_advertisement = context.Request["view_advertisement"];
            if (!string.IsNullOrEmpty(view_advertisement))
            {
                string Id = context.Request["Id"];
                RenderSingle(context, Convert.ToInt32(Id));
            }
            string list_advertisement = context.Request["list_advertisement"];
            if(!string.IsNullOrEmpty(list_advertisement)){
                RenderList(context);
            }
            string delete_advertisement = context.Request["delete_advertisement"];
            if (!string.IsNullOrEmpty(delete_advertisement)) {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.Delete<core_advertisement>(delete_advertisement);
                RenderList(context);
            }
            string lockunlock_advertisement = context.Request["lockunlock_advertisement"];
            if (!string.IsNullOrEmpty(lockunlock_advertisement)) {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.LockUnLock<core_event>(lockunlock_advertisement, "advertisement_active");
                RenderList(context);
            }
            #endregion
        }


        private void RenderSingle(HttpContext context, int Id)
        {
            BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
            object obj = bussiness.Get<core_advertisement>(
               c => c.advertisement_id.Equals(Id),
                c => new
                {
                    id = c.advertisement_id,
                    img = c.advertisement_img,
                    width = c.advertisement_width,
                    height = c.advertisement_height,
                    embed = c.advertisement_embed,
                    active = c.advertisement_active
                });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderList(HttpContext context)
        {
            BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
            IList<object> list = bussiness.List<core_advertisement>(
                null,
                null,
                null,
                null,
                 c => new
                 {
                     id = c.advertisement_id,
                     img = c.advertisement_img,
                     width = c.advertisement_width,
                     height = c.advertisement_height,
                     embed = c.advertisement_embed,
                     active = c.advertisement_active
                 });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);

        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}